package dnuifood_cjy.dnuifood_16110100529.controller;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import dnuifood_cjy.dnuifood_16110100529.R;
import dnuifood_cjy.dnuifood_16110100529.bean.ResultBean;
import dnuifood_cjy.dnuifood_16110100529.listener.OnRetrofitListener;
import dnuifood_cjy.dnuifood_16110100529.model.UserModel;

public class RegisterActivity extends BaseActivity implements OnRetrofitListener<ResultBean> {

    private Intent intent;
    private Button reg_return;
    private Button register;
    private EditText et_reg_user, et_reg_password, et_reg_repassword,
            et_reg_telephone, et_reg_adress, et_reg_comment;
    private UserModel userModel;

//   Bundle bundle = this.getIntent().getExtras();
  // String result = bundle.getString ("data");

    @Override
    void initViews() {
        setLayout(R.layout.activity_register);
        register = (Button) findViewById(R.id.reg_return);
        et_reg_user = (EditText) findViewById(R.id.et_reg_user);
        et_reg_password = (EditText) findViewById(R.id.et_reg_password);
        et_reg_repassword = (EditText) findViewById(R.id.et_reg_repassword);
        et_reg_telephone = (EditText) findViewById(R.id.et_reg_telephone);
        et_reg_adress = (EditText) findViewById(R.id.et_reg_adress);
        et_reg_comment = (EditText) findViewById(R.id.et_reg_comment);




    }

    @Override
   void initEvents() {
        //处理注册按钮
        register.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
               // bundle.putString ("data", "test");

                if (et_reg_password.getText().toString().equals(et_reg_repassword.getText().toString())) {
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    intent.putExtra("user", et_reg_user.getText().toString());
                    setResult(RESULT_OK, intent);
                    finish();
                    //register();
                }
                else {
                    //showToast("注册成功");
                    Toast.makeText(RegisterActivity.this,"请确认两次密码相同",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }


    private void register() {

        userModel = new UserModel();
        userModel.register(et_reg_user.getText().toString(),
                et_reg_password.getText().toString(),
                et_reg_telephone.getText().toString(),
                et_reg_adress.getText().toString(),
                et_reg_comment.getText().toString() ,this);
    }
    @Override
    void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();
        initEvents();
        initData();
    }



    @Override
    public void onSuccess(ResultBean resultBean, int flag) {

        if (resultBean.getSuccess().equals("1")) {
            intent = new Intent();
            intent.putExtra("user", et_reg_user.getText().toString());
            setResult(RESULT_OK, intent);
            finish();
            showToast("注册成功");
        } else
            {
                showToast("注册失败");
        }
    }




    @Override
    public void onFailure(String msg) {
        showToast("网络发生错误");
    }





}
